package ming.hello.controller;

import ming.hello.entry.UserInfo;
import ming.hello.service.UserInfoService;
import ming.hello.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * Created by jamy on 2020-09-03
 */
@Controller
public class HelloWorld {
    @Autowired
    private UserInfoService userInfoServiceImpl;

    @Autowired
    private RedisUtils redisUtils;

    @RequestMapping("/hello")
    @ResponseBody
    public String HelloWorld(){
        System.out.println("controller方法进来了");
        String value = (String) redisUtils.get("jamy");
        return "欢迎"+value+"来到这个大家庭";
    }

    // 查询单个用户
    @RequestMapping("/user")
    @ResponseBody
    public List<UserInfo> getUser(@RequestParam("id")String id){
        return userInfoServiceImpl.queryUser(id);
    }

    //查询所有用户
    @RequestMapping("/users")
    @ResponseBody
    public List<UserInfo> getUsers(){
        return userInfoServiceImpl.queryUsers();
    }

    // 根据ID和用户名查询用户
    @RequestMapping("/userByName")
    @ResponseBody
    public List<UserInfo> getUserByNameAndId(@RequestParam("id") String id, @RequestParam("name") String name){
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("name", name);
        return userInfoServiceImpl.queryUserByNameAndId(map);
    }

    // 新增用户
    @RequestMapping("/addUsers")
    @ResponseBody
    public String addUsers(){
        List<UserInfo> list = new ArrayList<>();
        UserInfo u1 = new UserInfo();
        u1.setName("song");
        u1.setAge(22);
        u1.setAddress("上海浦东新区松涛路80号");
        list.add(u1);
        UserInfo u2 = new UserInfo();
        u2.setName("ming");
        u2.setAge(25);
        u2.setAddress("上海浦东新区银丰路549弄138号");
        list.add(u2);
        UserInfo u3 = new UserInfo();
        u3.setName("Lucy");
        u3.setAge(21);
        u3.setAddress("北京海淀区");
        list.add(u3);
        userInfoServiceImpl.addUsers(list);
        return "新增用户成功!";
    }

    // 根据ID删除用户
    @RequestMapping("/delete")
    @ResponseBody
    public String deleteUser(@RequestParam("id") String id){
        userInfoServiceImpl.deleteUser(id);
        return "删除成功!!!";
    }


    // 根据名字修改地址
    @RequestMapping("/updateAddress")
    @ResponseBody
    public String updateAddressByName(@RequestParam("name") String name, @RequestParam("address") String address){
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("address", address);
        userInfoServiceImpl.updateAddress(map);
        return "地址修改成功!!!";
    }

    // 测试项缓存中放map
    @RequestMapping("/map")
    @ResponseBody
    public String setRedis(){
        Map<String, Object> map = new HashMap<>();
        map.put("k1", "1");
        map.put("k2", "2");
        map.put("k3", "3");
        map.put("k4", "4");
        map.put("k5", "5");
        redisUtils.hmSetAll("12345", map);
        return "map放入redis缓存成功!!!";
    }

    // 测获取redis缓存中放map
    @RequestMapping("/gmap")
    @ResponseBody
    public String getRedis(){


        Map<Object, Object> map = redisUtils.hmGetAll("12345");


        String v1 = (String) map.get("k1");
        return "map集合的值:"+map.toString()+" ,k1的值:"+v1;
    }

    @RequestMapping("/list")
    @ResponseBody
    public String setList(){
        redisUtils.add("list1", "123");
        redisUtils.add("list1", "12");
        redisUtils.add("list1", "123");
        redisUtils.add("list1", "10");
        redisUtils.add("list1", "10");

        return "list放入redis缓存成功!!!";
    }

    @RequestMapping("/glist")
    @ResponseBody
    public String getList(){
        Set set = redisUtils.setMembers("list1");
        return set.toString();
    }

}
